﻿ko.components.register('tree-widget', {
    viewModel: function (params) { 
        this.items = params.datasource;
        self.toggle = function (data) {
            data.expanded(!data.expanded);
            data.page_text('sfsfsf');
        };

        self.toggle_parent = function (data) {
            console.log(data);
            return true;
        };
        self.toggle_select = function (data) {
            var items = data.children();
            for (var i = 0; i < items.length; i++) {
                items[i].checked(data.checked());
            }
            return true;
        };
    },
    template: ' <ul id="browser" class="filetree" data-bind="foreach:items"><li data-bind="css:{expandable:children&&children.length>0&&(expanded)}" >\
<div class="hitarea collapsable-hitarea" data-bind="click:$component.toggle"></div><span><input type="checkbox"  data-bind="checked:checked,click:$component.toggle_select" /></span><span class="folder" data-bind="text:page_text"></span>\
    <ul  data-bind="foreach:children,visible:expanded"> <li class="last"><span><input type="checkbox" data-bind="checked:checked,click:$component.toggle_parent" /></span> <span class="file"  data-bind="text:page_text"></span></li> </ul>\
</li></ul>'
});


/*             my97datepicker  时间格式'yyyy-MM-dd HH-mm-ss'           */
ko.bindingHandlers.datetimes = {
    init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
        var allBindings = allBindingsAccessor();
        var value = ko.utils.unwrapObservable(valueAccessor());
        var date_format = allBindings.date_format || 'yyyy-MM-dd HH:mm:ss';
        $(element).attr("class", "Wdate");
        $(element).focus(function () {
            WdatePicker({
                isShowClear: true, readOnly: true, dateFmt: date_format
            });
        });
        $(element).val(value);
    },
    update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        var accessor=valueAccessor;
        $(element).val(value);
        $(element).blur(function () {
            var v = $(element).val();
            accessor()(v);
        });
    }
};

ko.bindingHandlers.select2 = {
    init: function (element, valueAccessor, allBindingsAccessor) {
        var obj = valueAccessor(),
            allBindings = allBindingsAccessor(),
            lookupKey = allBindings.lookupKey;

        setTimeout(function () {
            $(element).select2(obj);
        }, 0);

        if (lookupKey) {
            var value = ko.utils.unwrapObservable(allBindings.value);
            $(element).select2('data', ko.utils.arrayFirst(obj.data.results, function (item) {
                return item[lookupKey] === value;
            }));
        }

        ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
            $(element).select2('destroy');
        });
    },
    update: function (element) {
        $(element).trigger('change');
    }
};

